package fr.upstlse.glre.client.administration.communiquer;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.binder.DataListBinder;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.user.client.rpc.AsyncCallback;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.administration.communiquer.action.AdminFilterAction;
import fr.upstlse.glre.client.constante.AdminConstante;
import fr.upstlse.glre.client.constante.fields.CriteriaConstante;
import fr.upstlse.glre.client.espacededie.commun.utilisateur.AbstractFilterBar;
import fr.upstlse.glre.client.espacededie.commun.utilisateur.AbstractRemoveFilterAction;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.user.UserVOGWT;
import fr.upstlse.glre.client.widgets.modelData.CriteriaModelData;
import fr.upstlse.glre.client.widgets.modelData.UserModelData;

public class AdminFilterBar extends AbstractFilterBar {

	public AdminFilterBar() {
		super();
		setFilterAction(new AdminFilterAction());
		setSupprimerFilterAction(new RemoveFilterAction());
	}
	
	@Override
	protected ComboBox<CriteriaModelData> createCriteriaField() {
		ListStore<CriteriaModelData> lStore = new ListStore<CriteriaModelData>();
		lStore.add(getCriteriaModelDatas());

		ComboBox<CriteriaModelData> lComboBox = new ComboBox<CriteriaModelData>();
		lComboBox.setId(AdminConstante.COMMUNIQUER_FILTER_COMBOBOX_ID.getValue());
		lComboBox.setDisplayField(CriteriaConstante.MODELDATA_CRITERE.getValue());
		lComboBox.setWidth(210);
		lComboBox.setStore(lStore);
		return lComboBox;
	}

	@Override
	protected TextField<String> createCriteriaValue() {
		TextField<String> lTextField = new TextField<String>();
		lTextField.setId(AdminConstante.COMMUNIQUER_FILTER_TEXTFIELD_ID.getValue());
		lTextField.setName(AdminConstante.COMMUNIQUER_FILTER_TEXTFIELD_NAME.getValue());
		lTextField.setLabelStyle("display: none");
		return lTextField;
	}
	
	private List<CriteriaModelData> getCriteriaModelDatas() {
		ArrayList<CriteriaModelData> lCriteriaModelDatas = new ArrayList<CriteriaModelData>(2);
		lCriteriaModelDatas.add(new CriteriaModelData(AdminConstante.COMMUNIQUER_FILTER_CRITERE_LOGIN.getValue()));
		lCriteriaModelDatas.add(new CriteriaModelData(AdminConstante.COMMUNIQUER_FILTER_CRITERE_ROLE.getValue()));
		lCriteriaModelDatas.add(new CriteriaModelData(AdminConstante.COMMUNIQUER_FILTER_CRITERE_NOM.getValue()));
		
		return lCriteriaModelDatas;
	}
	
	class RemoveFilterAction extends AbstractRemoveFilterAction {

		@Override
		protected void action(ComponentEvent ce) {
			AsyncCallback<List<UserVOGWT>> lAllAsyncCallback = new AsyncCallback<List<UserVOGWT>>() {

				public void onFailure(Throwable pCaught) {
					MessageBox.alert("Erreur getAll ", pCaught.getMessage(),
							null);
				}

				public void onSuccess(List<UserVOGWT> result) {
					final ListStore<UserModelData> lStore = new ListStore<UserModelData>();
					lStore.add(UserModelData.toUserModelDatas(result));
					
					DataListBinder<UserModelData> binder = new DataListBinder<UserModelData>(
							Communiquer.getInstance().getListeUserPanel(), lStore);
					binder.setDisplayProperty(Communiquer.getInstance().getBinder().getDisplayProperty());
					binder.setStringProvider(new ModelStringProvider<ModelData>() {

								public String getStringValue(ModelData model,
										String property) {
									String lTexte = (String) model
											.get(RfgConstante.USER_MODELDATA_LOGIN);
									lTexte += " ("
											+ (String) model
													.get(RfgConstante.USER_MODELDATA_ROLE)
											+ ")";
									return lTexte;
								}

							});
					binder.init();
				}
			};
			ControlerUtil.getUserControlerServiceAsyncInstance().getAllUser(lAllAsyncCallback);
		}
	}
}
